﻿using Android.App;
using Android.Content;
using Android.Media;
using Android.OS;
using Android.Runtime;
using Android.Support.V7.Widget;
using Android.Views;
using Android.Widget;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using static Android.Media.MediaPlayer;

namespace YuRadio
{
    class StationAdapter : RecyclerView.Adapter,IOnPreparedListener
    {
        List<Station> Stations=new List<Station>();
        
        MediaPlayer player;
        RecyclerView recyclerView;
        StationViewHolder lastItem;
        public StationAdapter(Station[] stations, MediaPlayer mplayer)
        {
            Stations.AddRange(stations);
            this.player = mplayer;
        }

        public override int ItemCount => Stations.Count;

        public override void OnAttachedToRecyclerView(RecyclerView recyclerView)
        {
            this.recyclerView = recyclerView;
        }
        

        public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
        {
            var stholder = holder as StationViewHolder;
            stholder.Caption.Text = Stations[position].Name;
            stholder.ItemView.Click += (s,e)=> {
                clearLast();
                lastItem = stholder;
                if (this.player != null)
                {
                    //if(this.player.IsPlaying) this.player.Stop();
                    this.player.Release();
                    this.player = null;
                }

                this.player = new MediaPlayer();
                this.player.SetOnPreparedListener(this);
                try
                {
                    this.player.SetDataSource(Stations[position].Url);
                    this.player.PrepareAsync();
                }
                catch (Exception ex)
                {
                    
                }
                if (position<Stations.Count)
                {
                    stholder.Caption.Text = Stations[position].Name + "  ---";
                    stholder.Caption.SetBackgroundColor(new Android.Graphics.Color(0xa0, 0xc0, 0xff));
                }
            };

        }

        private void clearLast()
        {
            if (lastItem != null)
            {
                lastItem.Caption.Text = Stations[lastItem.Position].Name;
                lastItem.Caption.SetBackgroundColor(new Android.Graphics.Color(0xf0, 0xf0, 0xf0));
            }
        }

        internal void UpdateItem(Station station)
        {
            if (lastItem != null)
            {
                Stations[lastItem.Position] = station;
                NotifyItemChanged(lastItem.Position);
                StationManager.saveStations(Stations);
            }
        }

        internal void AddItem(Station added)
        {
            Stations.Add(added);
            NotifyItemInserted(ItemCount);
            StationManager.saveStations(Stations);
            //NotifyItemChanged(ItemCount);
            //NotifyItemInserted(Stations.Count - 1);
            //NotifyDataSetChanged();
        }

        public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
        {
            View itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.station_item, parent, false);
            StationViewHolder stholder = new StationViewHolder(itemView);
            
            return stholder;
        }

        internal void Stop()
        {
            clearLast();
            this.player.Stop();
        }

        internal Station getCurrent()
        {
            if (lastItem == null)
                return null;
            else
                return Stations[lastItem.Position];
        }

        public void OnPrepared(MediaPlayer mp)
        {
            mp.Start();
        }

        public void DeleteCurrent()
        {
            if (lastItem != null)
            {
                Stations.RemoveAt(lastItem.Position);
                NotifyItemRemoved(lastItem.Position);
                StationManager.saveStations(Stations);
                lastItem.Dispose();
                lastItem = null;
            }
        }
    }

    class StationViewHolder : RecyclerView.ViewHolder
    {
        public ImageView Image { get; set; }
        public TextView Caption { get; set; }
        
        public StationViewHolder(View itemView) : base(itemView)
        {
            
            this.Image = itemView.FindViewById<ImageView>(Resource.Id.station_icon);
            this.Caption = itemView.FindViewById<TextView>(Resource.Id.station_name);
            
        }

        
    }
}